『Kubernetes on AWS』
書籍URL
読む目的
仕事でk8sを使いそうなので、基本ぐらいは身につけたい。 開始日: 2025-08-11
終了日: YYYY-MM-DD
状態: ペンディング(EKSのコストが高くてコツコツ続けにくい)
コンテナオーケストレーション
複数のコンテナが1つのサービスを構成するときに特に必要
コンテナ間通信と可用性の確保のため
k8s
Google製(?)
Borgが期限
k8sのv1.0リリースと同時にGoogleがLinux FoundationとともにCNCFを設立し、k8sを寄贈した コンセプト: データプレーンとコントロールプレーン
複数のサーバー(データプレーン)を1つのマシンリソースとして抽象化
コントロールプレーンでコンテナの稼働状況の監視・制御
基本オブジェクト
Pod
k8sの最小単位
1つ以上のコンテナからなる
ReplicaSet
Podをいくつ起動するか
Deployment
デプロイの単位
Service
デプロイしたPodをk8s外部へ公開するための仕組み
いくつかのタイプがあり、ロードバランシングが代表的
code:basic-objects.mmd
graph LR
subgraph K8s Cluster
Service
subgraph Deployment
subgraph ReplicaSet
end
end
end
client-->Service<-->|related by label|Deployment
EKS
AWSマネージドなk8s コントロールプレーンを提供してくれる
VPCとの統合(通常はクラスター内ネットワークを意識する必要あり)
データプレーンの選択肢
EC2
独立したEC2 or マネージド型ノードグループ
Fargate
GitHub上でロードマップが公開されている。
サンプルアプリ構成図
code:sample-arch.mmd
graph LR
subgraph S3
end
subgraph VPC
bastion
ELB
C-plane
subgraph worker1
end
subgraph worker2
end
end
browser-->|access to frontend|cloudfront-->frontappbucket
browser-->|access to api|ELB-->api1-->rds
ELB-->api2-->rds
batch1-->rds
batch1-->batchappbucket
batch2-->rds
batch2-->batchappbucket
bastion-->rds
eksctl create cluster でクラスターの新規作成
書籍だと--versionが1.14だがno longer supportedと出たので、候補の中で一番古い1.28を指定した
環境構築めっちゃ時間かかりそう(書籍によると20分)
2025-08-13 02:08:16 [ℹ] waiting for CloudFormation stack "eksctl-eks-work-cluster-cluster" というログが出ているので、CloudFormationテンプレートによってデプロイされているっぽい
作られつつあるリソースたち
https://scrapbox.io/files/689b75a764f76998743728f4.png
kubectl get nodes したら以下のエラーメッセージが出た
$ kubectl get nodes
Unable to connect to the server: getting credentials: exec plugin is configured to use API version client.authentication.k8s.io/v1beta1, plugin returned version client.authentication.k8s.io/v1alpha1
ChatGPT曰く
kubeconfig 側(kubectl が読む設定) → client.authentication.k8s.io/v1beta1 を期待している
実際の認証プラグイン(AWS CLI, gcloud, Azure CLI など)→ client.authentication.k8s.io/v1alpha1 を返している
というバージョンの不整合が起きています。
pipか何かで入れたawscliが古かったようなので、brewで入れ直したら想定出力を得た。 $ kubectl get nodes
NAME STATUS ROLES AGE VERSION
ip-192-168-1-182.ap-northeast-1.compute.internal Ready <none> 7d23h v1.28.15-eks-3abbec1
ip-192-168-2-49.ap-northeast-1.compute.internal Ready <none> 7d23h v1.28.15-eks-3abbec1
Nginxのdeploymentができた
$ kubectl apply -f 02_nginx_deployment_cpu200_k8s.yaml
deployment.apps/nginx created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6f79d7f97d-25qng 1/1 Running 0 43s
nginx-6f79d7f97d-97g6w 1/1 Running 0 43s
なんか出力が違うなと思ったら見間違えていたのでdeploymentを削除
$ kubectl delete deployment nginx
あらためてデプロイ
$ kubectl apply -f 02_nginx_k8s.yaml
Nginx見られた
$ kubectl port-forward nginx-pod 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from ::1:8080 -> 80 Handling connection for 8080
Handling connection for 8080
削除
$ kubectl delete nginx-pod
RDSでPostgreSQLを立てる
CloudFormationのエラー
Resource handler returned message: "Cannot find version 10.21 for postgres (Service: Rds, Status Code: 400, Request ID: 5e5fc01f-dd8e-461d-a02b-4adb597abc78) (SDK Attempt Count: 1)"
今一番古いのが11.9っぽいので、テンプレートをこれに書き換える。
$ aws rds describe-db-engine-versions --engine aurora-postgresql --query '*[].EngineVersion' --output text --region ap-northeast-1 11.9
またエラー。なんで???
Resource handler returned message: "Cannot find version 11.9 for postgres (Service: Rds, Status Code: 400, Request ID: 245bc095-8f1a-41cf-b9aa-4adaeacc60b8) (SDK Attempt Count: 1)"
マネコンで見たら11.9はなかった。AWSCLIが古いとか?(APIからリストを取って来てそうなものだが
https://scrapbox.io/files/68c96f5417d03e5f6a5cedad.png
エラー。だるすぎる
Resource handler returned message: "RDS does not support creating a DB instance with the following combination: DBInstanceClass=db.t2.micro, Engine=postgres, EngineVersion=13.15, LicenseModel=postgresql-license. For supported combinations of instance class and database engine version, see the documentation. (Service: Rds, Status Code: 400, Request ID: a1eee44a-8dd2-45e1-b057-9162864db918) (SDK Attempt Count: 1)" (RequestToken: 60435d9f-0fe4-6416-6117-4c9889b28c53, HandlerErrorCode: InvalidRequest)
インスタンスクラスをdb.m5.largeに書き換えて再挑戦
DBParameterGroupのFamilyが書き変わっておらずまたエラー。書き換えて再挑戦
$ aws rds describe-db-engine-versions --query "DBEngineVersions[].DBParameterGroupFamily" --engine postgres
勝った
https://scrapbox.io/files/68c97a19f1d3fe9ecff143c1.png
SSMで踏み台サーバーにも入れた。ポスグレのinstallはバージョンを読み替えた。
sh-4.2$ sudo amazon-linux-extras install -y postgresql13
JAVA_HOMEの設定時間かかった
brew install corretto@11
export JAVA_HOME=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
眠いのでおわり
ほとんどk8s触れてないやhんけ